package ch.zbw.pra.sysventory.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import ch.zbw.pra.sysventory.SysventoryApp;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.ui.gui.menu.MenuElements;
import ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.OverViewAndSearchPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.scanpanel.ScanPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.statisticpanel.StatisticPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.ueberpanel.UeberPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.welcomepanel.WelcomePanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class MainFrame extends JFrame implements WindowListener {

	private static final long serialVersionUID = -428025890515408915L;

	public enum PanelEnum {
		WELCOME, SCAN, SEARCH, SETTINGS, DETAIL, UEBER, STATISTIC
	};

	private JPanel cardPanel;
	private MenuElements menu;
	private WelcomePanel welcomePanel;
	private OverViewAndSearchPanel overviewAndSearchPanel;
	private ScanPanel scanPanel;
	private SettingsPanel settingsPanel;
	private UeberPanel ueberPanel;
	private StatisticPanel statisticPanel;

	public MainFrame(String title) {
		super(title + "   >> MainFrame <<");
		init();

	}

	public void init() {
		this.cardPanel = new JPanel(new CardLayout());
		this.menu = new MenuElements(this.cardPanel);
		this.setJMenuBar(this.menu);
		this.welcomePanel = new WelcomePanel();
		this.overviewAndSearchPanel = new OverViewAndSearchPanel();
		this.scanPanel = new ScanPanel();
		this.settingsPanel = new SettingsPanel();
		this.ueberPanel = new UeberPanel();
		this.statisticPanel = new StatisticPanel();
		this.cardPanel.add(this.welcomePanel, PanelEnum.WELCOME.toString());
		this.cardPanel.add(this.scanPanel, PanelEnum.SCAN.toString());
		this.cardPanel.add(this.overviewAndSearchPanel, PanelEnum.SEARCH.toString());
		this.cardPanel.add(this.settingsPanel, PanelEnum.SETTINGS.toString());
		this.cardPanel.add(this.ueberPanel, PanelEnum.UEBER.toString());
		this.cardPanel.add(this.statisticPanel, PanelEnum.STATISTIC.toString());
		this.setLayout(new BorderLayout());
		this.add(this.cardPanel, BorderLayout.CENTER);
		try {
			this.setIconImage(new ImageIcon(new FileGenerator().getFile(FileType.ICON_PGS).getAbsolutePath()).getImage());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Bild laden fehlgeschlagen");
		}
		this.addWindowListener(this);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		GuiLayout.centerWindow(this);
		this.setVisible(true);
	}

	public MenuElements getMenuInstance() {
		return this.menu;
	}

	@Override
	public String getTitle() {
		return super.getTitle();
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosing(WindowEvent e) {
		SysventoryApp.saveBeforeClose();
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}
}
